#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    using PII = pair<int, int>;
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc,
        int color) {
        int prev = image[sr][sc];
        if (prev == color) return image;
        queue<PII>q;
        q.push({ sr,sc });
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            image[a][b] = color;
            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < image.size() && y >= 0 && y < image[0].size() && image[x][y] == prev)
                    q.push({ x,y });
            }
        }
        return image;
    }

};